package org.campuscontent.ui.multipanel;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

import org.campuscontent.ui.multipanel.UsersPanelManager.Entry;

public class UsersPanelList extends JList
{
    public UsersPanelList()
    {
        setCellRenderer( new UsersPanelListRenderer() );

        init();
    }

    private void init()
    {
        UsersPanelManager upMng = UsersPanelManager.getInstance();
        setListData( upMng.elements() );

        MouseListener mouseListener = new MouseAdapter()
        {
            public void mouseClicked( MouseEvent e )
            {
                if ( e.getClickCount() == 1 )
                {
                    int index = locationToIndex( e.getPoint() );
                    Object obj = getModel().getElementAt( index );
                    
                    if ( obj != null )
                    {
                        Entry et = (Entry)obj;
                        et.state = !et.state; 
                        
                        redraw();
                    }
                }
            }
        };
        addMouseListener( mouseListener );
        
        setSelectionMode( ListSelectionModel.SINGLE_SELECTION  );
    }
    
    public void redraw()
    {
        invalidate();
        validate();
        repaint();
        
        PopupMenuConnector conn = PopupMenuConnector.getInstance();
        if ( conn.container != null )
        {
            conn.container.updateItems();
        }
    }

    class UsersPanelListRenderer extends JPanel implements ListCellRenderer
    {
        public UsersPanelListRenderer()
        {
            setLayout( new FlowLayout( FlowLayout.LEFT ) );
            
            check = new JCheckBox();
            label = new JLabel();

            add( check );
            add( label );
        }

        public Component getListCellRendererComponent( JList list, // the list
        Object value, // value to display
        int index, // cell index
        boolean isSelected, // is the cell selected
        boolean cellHasFocus ) // does the cell have focus
        {
            Entry s = (Entry) value;
            label.setText( s.instance.label() );
            check.setSelected( s.state );

            if ( isSelected )
            {
                setBackground( list.getSelectionBackground() );
                setForeground( list.getSelectionForeground() );
            }
            else
            {
                setBackground( list.getBackground() );
                setForeground( list.getForeground() );
            }

            setEnabled( list.isEnabled() );
            setFont( list.getFont() );
            setOpaque( true );

            return this;
        }

        private JCheckBox check;
        private JLabel label;
    }

    ImageIcon longIcon = new ImageIcon( getClass().getResource( "/org/campuscontent/images/toolbar/panels.png" ) );
}
